var apiUrl = "http://localhost:5000";


var AjaxIO = {


    _getUrl: function (action) {

        return apiUrl + action;

    },


    _ajax: function (url, method, param, callback, fail_callback) {


        if (callback == undefined) {

            callback = param;

        }

        var token = localStorage.getItem('token');

        var headers = undefined;

        if (token != null) {

            // headers = {'Authorization': 'Basic ' + token};

            param.Authorization = 'Basic ' + token;

        }

        $.ajax({

            url: this._getUrl(url),

            data: param,

            datatype: 'json',

            // headers:headers,

            method: method,

            statusCode: {

                401: function (result) {
                },

                404: function () {


                },

                201: function () {


                },

                202: function () {


                }

            },

            success: callback,

            error: fail_callback

        });

    },


    getJSON: function (url, param, callback, fail_callback) {


        this._ajax(url, "GET", param, callback, fail_callback);


    },


    post: function (url, param, callback, fail_callback) {


        this._ajax(url, "POST", param, callback, fail_callback)

    }

};

function Loginweb() {
    var info =
        {
            username: document.getElementsByName('loginname')[0].value,
            password: document.getElementsByName('loginpassword')[0].value,
        };


    AjaxIO.post("/loginweb", info, function (result) {
            alert("success!");
            $("#pp1").css("display", "none");
            $("#login1").html("更换账号");
            var imgpath = result.imgpath;
            var motto = result.motto;
            if (result.token) {

                localStorage.setItem("token", result.token);
                document.getElementById('imgpath').src = imgpath;
                document.getElementById('motto').innerHTML = motto;

            }

        },
        function (result) {
            alert("账号密码错误!");
        }
    )
}
function mystatus() {
    var info = {
        token: localStorage.getItem('token'),
    };

    AjaxIO.post("/mystatus", info, function (result) {
            var work1 = result.work1;
            var work2 = result.work2;
            var imgstat = result.imgstatus;
            if (work1 == 'ok') {
                $("#status1").css("width", "35%");
            }
            if (work2 == 'ok') {
                $("#status2").css("width", "35%");
            }
            if (imgstat == 'ok') {
                $("#status3").css("width", "30%");
            }
        },
        function (result) {
            alert("账号密码错误！");
        }
    )
}


$(document).ready(function () {
    if (localStorage.getItem('token')) {
        $("#login1").html("更换账号");
    }

});
